package com.wd.amodel.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.wd.amodel.R;
import com.wd.amodel.listener.ImageClickListener;
import com.wd.base.util.Logger;
import com.wd.common.glide.LbGlide;

import java.util.List;

/**
 * @author W4255
 * @version [V1.0.0.000, 2021/1/27 0:36]
 * @Description: NA
 */
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ItemViewHolder> {
    private FragmentActivity mContext;
    private List<String> imageLst;
    private ImageClickListener listener;

    public ImageAdapter(FragmentActivity mContext, List<String> list) {
        this.mContext = mContext;
        this.imageLst = list;
    }

    public void setListener(ImageClickListener listener){
        this.listener = listener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(mContext)
                .inflate(R.layout.layout_item_image, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.imageView.setTag(R.id.iv_img, position);
        if (imageLst == null || imageLst.size() <= position) {
            Logger.w("loadImage, imageLst == null or imageLst.size <= position");
            return;
        }
        loadImage(position, holder.imageView);
        holder.textView.setText(imageLst.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(position, imageLst.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return imageLst != null ? imageLst.size() : 0;
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView textView;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_img);
            textView = itemView.findViewById(R.id.tv_text);
        }
    }

    /**
     * 加载图片
     *
     * @param position
     * @param imageView
     */
    private void loadImage(int position, ImageView imageView) {
        if (imageView == null) {
            Logger.w("loadImage, imageView == null");
            return;
        }
        String url = imageLst.get(position);
        if (TextUtils.isEmpty(url)) {
            Logger.w("loadImage, url == null");
            return;
        }
        LbGlide.with(mContext)
                .loading(R.mipmap.ic_launcher)
                .load(url)
                .into(imageView);
    }
}
